<?php

/**
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.niushop.com.cn
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 */

namespace app\admin\controller;

use app\model\goods\Goods as GoodsModel;
use app\model\goods\GoodsBrand as GoodsBrandModel;
use app\model\system\Floor;
use app\model\system\FloorBlock;
use app\model\web\Pc as PcModel;

/**
 * Pc端 控制器
 */
class Pc extends BaseAdmin
{
	private $pc_model;

	public function __construct()
	{
		$this->pc_model = new PcModel();
		parent::__construct();
	}

	/**
	 * 热门搜索关键词
	 * @return mixed
	 */
	public function hotSearchWords()
	{
		if (request()->isAjax()) {
			$data = [
				'words' => input("words", "")
			];
			$res = $this->pc_model->setHotSearchWords($data);
			return $res;
		} else {
			$hot_search_words = $this->pc_model->getHotSearchWords();
			$hot_search_words = $hot_search_words['data']['value'];
			$this->assign("hot_search_words", $hot_search_words);
			return $this->fetch('pc/hot_search_words');
		}
	}

	/**
	 * 默认搜索关键词
	 * @return mixed
	 */
	public function defaultSearchWords()
	{
		if (request()->isAjax()) {
			$data = [
				'words' => input("words", "")
			];
			$res = $this->pc_model->setDefaultSearchWords($data);
			return $res;
		} else {
			$default_search_words = $this->pc_model->getDefaultSearchWords();
			$default_search_words = $default_search_words['data']['value'];
			$this->assign("default_search_words", $default_search_words);
			return $this->fetch('pc/default_search_words');
		}
	}

	/**
	 * 首页浮层
	 * @return mixed
	 */
	public function floatLayer()
	{
		if (request()->isAjax()) {
			$data = [
				'title' => input("title", ""),
				'url' => input("url", ""),
				'is_show' => input("is_show", 1),
				'number' => input("number", ""),
				'img_url' => input("img_url", "")
			];
			$res = $this->pc_model->setFloatLayer($data);
			return $res;
		} else {
			$link = $this->pc_model->getLink();
			$this->assign("link", $link);
			$float_layer = $this->pc_model->getFloatLayer();
			$float_layer = $float_layer['data']['value'];
			$this->assign("float_layer", $float_layer);
			return $this->fetch('pc/float_layer');
		}
	}


	/**
	 * 导航设置
	 * @return mixed
	 */
	public function navList()
	{
		if (request()->isAjax()) {
			$page = input('page', 1);
			$page_size = input('page_size', PAGE_LIST_ROWS);
			$search_text = input('search_text', '');

			$condition = [];
			$condition[] = ['nav_title', 'like', '%' . $search_text . '%'];
			$order = 'create_time desc';

			$model = new PcModel();
			return $model->getNavPageList($condition, $page, $page_size, $order);
		} else {
			return $this->fetch('pc/nav_list');
		}
	}

	/**
	 * 添加导航
	 * @return mixed
	 */
	public function addNav()
	{
		$model = new PcModel();
		if (request()->isAjax()) {
			$data = [
				'nav_title' => input('nav_title', ''),
				'nav_url' => input('nav_url', ''),
				'sort' => input('sort', ''),
				'is_blank' => input('is_blank', ''),
				'nav_icon' => input('nav_icon', ''),
				'is_show' => input('is_show', ''),
				'create_time' => time(),
				'modify_time' => time(),
			];

			return $model->addNav($data);
		} else {
			$link_list = $model->getLink();
			$this->assign('link', $link_list);

			return $this->fetch('pc/add_nav');
		}
	}

	/**
	 * 编辑导航
	 * @return mixed
	 */
	public function editNav()
	{
		$model = new PcModel();
		if (request()->isAjax()) {
			$data = [
				'nav_title' => input('nav_title', ''),
				'nav_url' => input('nav_url', ''),
				'sort' => input('sort', ''),
				'is_blank' => input('is_blank', ''),
				'nav_icon' => input('nav_icon', ''),
				'is_show' => input('is_show', ''),
				'create_time' => time(),
				'modify_time' => time(),
			];
			$id = input('id', 0);
			$condition = [['id', '=', $id]];

			return $model->editNav($data, $condition);
		} else {
			$link_list = $model->getLink();
			$this->assign('link', $link_list);

			$id = input('id', 0);
			$this->assign('id', $id);

			$nav_info = $model->getNavInfo($id);
			$this->assign('nav_info', $nav_info['data']);

			return $this->fetch('pc/edit_nav');
		}
	}

	/**
	 * 删除导航
	 * @return mixed
	 */
	public function deleteNav()
	{
		if (request()->isAjax()) {
			$id = input('id', 0);
			$model = new PcModel();
			return $model->deleteNav([['id', '=', $id]]);
		}
	}

	/**
	 * 修改排序
	 */
	public function modifySort()
	{
		if (request()->isAjax()) {
			$sort = input('sort', 0);
			$id = input('id', 0);
			$model = new PcModel();
			return $model->modifyNavSort($sort, $id);
		}
	}


	/**
	 * 友情链接
	 * @return mixed
	 */
	public function linklist()
	{
		if (request()->isAjax()) {
			$page = input('page', 1);
			$page_size = input('page_size', PAGE_LIST_ROWS);
			$search_text = input('search_text', '');

			$condition = [];
			$condition[] = ['link_title', 'like', '%' . $search_text . '%'];
			$order = 'link_sort desc';

			$model = new PcModel();
			return $model->getLinkPageList($condition, $page, $page_size, $order);
		} else {
			return $this->fetch('pc/link_list');
		}
	}

	/**
	 * 添加友情链接
	 * @return mixed
	 */
	public function addLink()
	{
		$model = new PcModel();
		if (request()->isAjax()) {
			$data = [
				'link_title' => input('link_title', ''),
				'link_url' => input('link_url', ''),
				'link_pic' => input('link_pic', ''),
				'link_sort' => input('link_sort', ''),
				'is_blank' => input('is_blank', ''),
				'is_show' => input('is_show', ''),
			];

			return $model->addLink($data);
		} else {
			return $this->fetch('pc/add_link');
		}
	}

	/**
	 * 编辑友情链接
	 * @return mixed
	 */
	public function editLink()
	{
		$model = new PcModel();
		if (request()->isAjax()) {
			$data = [
				'link_title' => input('link_title', ''),
				'link_url' => input('link_url', ''),
				'link_pic' => input('link_pic', ''),
				'link_sort' => input('link_sort', ''),
				'is_blank' => input('is_blank', ''),
				'is_show' => input('is_show', ''),
			];
			$id = input('id', 0);
			$condition = [['id', '=', $id]];

			return $model->editLink($data, $condition);
		} else {

			$id = input('id', 0);
			$this->assign('id', $id);

			$link_info = $model->getLinkInfo($id);
			$this->assign('link_info', $link_info['data']);

			return $this->fetch('pc/edit_link');
		}
	}

	/**
	 * 删除友情链接
	 * @return mixed
	 */
	public function deleteLink()
	{
		if (request()->isAjax()) {
			$id = input('id', 0);
			$model = new PcModel();
			return $model->deleteLink([['id', '=', $id]]);
		}
	}

	/**
	 * 修改排序
	 */
	public function modifyLinkSort()
	{
		if (request()->isAjax()) {
			$sort = input('sort', 0);
			$id = input('id', 0);
			$model = new PcModel();
			return $model->modifyLinkSort($sort, $id);
		}
	}

	/**
	 * 获取楼层模板列表
	 *
	 * @return mixed
	 */
	public function floorBlocks()
	{
		return (new FloorBlock)->getList();
	}

	/**
	 * 楼层详情
	 * @return void
	 */
	public function floorDetail()
	{
		if (request()->isAjax()) {
			$res = (new Floor)->getDetail(input('floor_id', 0));
			return $res;
		}
	}

	public function getGoodsSkuInfo()
	{
		$sku_id = input('sku_id', "");
		$goods = new GoodsModel();
		$field = 'goods_id,sku_id,sku_name,price,market_price,discount_price,stock,sale_num,sku_image,goods_name,site_id,website_id,is_own,is_free_shipping,introduction,promotion_type';
		$list = $goods->getGoodsSkuInfo([['sku_id', '=', $sku_id]], $field);
		return $list;
	}

	public function getGoodsSkuList()
	{
		$sku_ids = input('sku_ids', "");
		$goods = new GoodsModel();
		$field = 'goods_id,sku_id,sku_name,price,market_price,discount_price,stock,sale_num,sku_image,goods_name,site_id,website_id,is_own,is_free_shipping,introduction,promotion_type';
		$order = 'sort desc,create_time desc';
		$list = $goods->getGoodsSkuList([['sku_id', 'in', $sku_ids]], $field, $order);
		return $list;
	}

	public function getBrandList()
	{
		$brand_ids = input('brand_ids', "");
		$condition = [
			['brand_id', 'in', $brand_ids]
		];
		$goods_brand_model = new GoodsBrandModel();
		$list = $goods_brand_model->getBrandList($condition, 'brand_id, brand_name, brand_initial,site_id, image_url', 'sort desc,create_time desc');
		return $list;
	}

	public function getBrandInfo()
	{
		$brand_id = input('brand_id', "");
		$condition = [
			['brand_id', '=', $brand_id]
		];
		$goods_brand_model = new GoodsBrandModel();
		$list = $goods_brand_model->getBrandInfo($condition, 'brand_id, brand_name, brand_initial,site_id, image_url');
		return $list;
	}

	/**
	 * 首页楼层
	 *
	 * @return void
	 */
	public function floor()
	{
		if (request()->isAjax()) {
			$page = input('page', 1);
			return (new Floor)->getFloorList($this->site_id, '', $page);
		}

		return $this->fetch();
	}


	public function test()
	{
		$goods = new GoodsModel();
		$goods_brand_model = new GoodsBrandModel();
		$res = (new Floor)->getDetail(74);
		if (!empty($res['data'])) {
			$json = $res['data']['config'];
			if (!empty($json)) {
				$json = json_decode($json, true);
				//				$json = json_decode('{"index":{"type":"text","value":{"text":"1F","link":""}},"leftLeftImage":{"type":"img","value":{"url":"upload/common/images/20200709/20200709034730159428085005764.png","link":""}},"leftRightBrands":{"type":"brand","value":{"brand_ids":"62,63,64,65,66","list":[]}},"rightCargos":{"type":"goods","value":{"sku_ids":"758,757,750","list":[]}}}', true);
				foreach ($json as $k => $v) {
					if (!empty($v['type'])) {
						if ($v['type'] == 'goods') {
							$field = 'goods_id,sku_id,sku_name,price,market_price,discount_price,stock,sale_num,sku_image,goods_name,site_id,website_id,is_own,is_free_shipping,introduction,promotion_type';
							$order = 'sort desc,create_time desc';
							$list = $goods->getGoodsSkuList([['sku_id', 'in', $v['value']['sku_ids']]], $field, $order);
							$list = $list['data'];
							$json[$k]['value']['list'] = $list;
						} elseif ($v['type'] == 'brand') {
							$condition = [
								['brand_id', 'in', $v['value']['brand_ids']]
							];
							$list = $goods_brand_model->getBrandList($condition, 'brand_id, brand_name, brand_initial,site_id, image_url', 'sort desc,create_time desc');
							$list = $list['data'];
							$json[$k]['value']['list'] = $list;
						}
					}
				}
				$res['data']['config'] = $json;
			}
		}
	}

	/**
	 * 编辑楼层
	 *
	 * @return void
	 */
	public function flooredit()
	{
		if (request()->isAjax()) {
			$block_id = input('block_id', '');
			$title = input('title', '');
			$index = input('index', '');
			$config = input('config', '');

			$floor_id = input('floor_id', 0);
			$state = input('state', 1);

			if (!empty($floor_id)) return (new Floor)->editFloor($this->site_id, $floor_id, $title, $block_id, $index, $config, $state);
			return (new Floor)->addFloor($this->site_id, $title, $block_id, $index, $config);
		}

		$this->forthMenu();
		return $this->fetch();
	}

	/**
	 * 删除楼层
	 *
	 * @return void
	 */

	public function floordel()
	{
		if (request()->isAjax()) {
			$floor_id = input('floor_id', 0);

			return (new Floor)->delFloor($this->site_id, $floor_id);
		}

		$this->forthMenu();
		return $this->fetch();
	}

	/**
	 * 获取楼层模板
	 *
	 * @return void
	 */
	public function getFloorBlocks()
	{
	}
}
